/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  com.sun.javafx.util.Utils
 *  javafx.animation.Interpolator
 *  javafx.animation.KeyFrame
 *  javafx.animation.KeyValue
 *  javafx.animation.Timeline
 *  javafx.beans.property.ObjectProperty
 *  javafx.beans.property.SimpleObjectProperty
 *  javafx.beans.value.WritableValue
 *  javafx.geometry.HPos
 *  javafx.geometry.Point2D
 *  javafx.geometry.Pos
 *  javafx.geometry.VPos
 *  javafx.scene.Node
 *  javafx.scene.layout.Region
 *  javafx.scene.layout.StackPane
 *  javafx.stage.Popup
 *  javafx.util.Duration
 */
package com.gys.common.data;

import com.gys.common.enums.PopupPos;
import com.gys.common.util.RegionUtil;
import com.sun.javafx.util.Utils;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.WritableValue;
import javafx.geometry.HPos;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Popup;
import javafx.util.Duration;

public class LPopup {
    protected static final Interpolator WEB_EASE = Interpolator.SPLINE((double)0.25, (double)0.1, (double)0.25, (double)1.0);
    private ObjectProperty<Node> popupNodeProperty = new SimpleObjectProperty();
    private Popup popup = new Popup();
    private PopupPos popupPos;
    private StackPane popupContentPane = new StackPane();
    private Region region;

    public LPopup(Region region, PopupPos popupPos) {
        this.region = region;
        this.popupPos = popupPos;
        this.initEvent();
    }

    public void setPopupContentPane(Region popupNode) {
        this.popupNodeProperty.set(popupNode);
    }

    public Node getPopupNode() {
        return (Node)this.popupNodeProperty.get();
    }

    public ObjectProperty<Node> popupNodeProperty() {
        return this.popupNodeProperty;
    }

    private void initEvent() {
        this.popupContentPane.setAlignment(Pos.TOP_LEFT);
        this.popupContentPane.getStyleClass().add("popup-pane");
        this.popup.setAutoHide(true);
        this.popupNodeProperty.addListener((l, o, n) -> {
            this.popup.getContent().clear();
            this.popupContentPane.getChildren().add(n);
            this.popup.getContent().add(this.popupContentPane);
        });
        this.popupContentPane.heightProperty().addListener(l -> this.show());
        this.popupContentPane.widthProperty().addListener(l -> this.show());
    }

    public Popup getPopup() {
        return this.popup;
    }

    public void show() {
        this.sizePopup();
        Point2D p = this.getPrefPopupPosition();
        double anchorX = RegionUtil.calcAnchorX(this.region, this.popupPos, (Node)this.popupContentPane);
        double anchorY = RegionUtil.calcAnchorY(this.region, this.popupPos, (Node)this.popupContentPane);
        this.popup.show(this.region.getScene().getWindow(), anchorX, anchorY);
        this.animation();
        this.popup.requestFocus();
    }

    public boolean isShowing() {
        return this.popup.isShowing();
    }

    public void hide() {
        this.popup.hide();
    }

    private void animation() {
        StackPane node = this.popupContentPane;
        Timeline timeline = new Timeline(new KeyFrame[]{new KeyFrame(Duration.millis((double)0.0), new KeyValue[]{new KeyValue((WritableValue)node.visibleProperty(), true), new KeyValue((WritableValue)node.opacityProperty(), 0, WEB_EASE), new KeyValue((WritableValue)node.scaleXProperty(), 0.6, WEB_EASE), new KeyValue((WritableValue)node.scaleYProperty(), 0.6, WEB_EASE)}), new KeyFrame(Duration.millis((double)100.0), new KeyValue[]{new KeyValue((WritableValue)node.scaleXProperty(), 1, WEB_EASE), new KeyValue((WritableValue)node.scaleYProperty(), 1, WEB_EASE)})});
        timeline.play();
    }

    private void sizePopup() {
        StackPane popupContent = this.popupContentPane;
        if (popupContent instanceof Region) {
            Region r = (Region)popupContent;
            double prefHeight = RegionUtil.snapSize(this.region, r.prefHeight(0.0));
            double minHeight = RegionUtil.snapSize(this.region, r.minHeight(0.0));
            double maxHeight = RegionUtil.snapSize(this.region, r.maxHeight(0.0));
            double h = RegionUtil.snapSize(this.region, Math.min(Math.max(prefHeight, minHeight), Math.max(minHeight, maxHeight)));
            double prefWidth = RegionUtil.snapSize(this.region, r.prefWidth(h));
            double minWidth = RegionUtil.snapSize(this.region, r.minWidth(h));
            double maxWidth = RegionUtil.snapSize(this.region, r.maxWidth(h));
            double w = RegionUtil.snapSize(this.region, Math.min(Math.max(prefWidth, minWidth), Math.max(minWidth, maxWidth)));
            popupContent.resize(w, h);
        } else {
            popupContent.autosize();
        }
    }

    private Point2D getPrefPopupPosition() {
        return Utils.pointRelativeTo((Node)this.region, (Node)this.popupContentPane, (HPos)HPos.CENTER, (VPos)VPos.BOTTOM, (double)0.0, (double)0.0, (boolean)true);
    }
}

